import Vuex from 'vuex'
import Vue from 'vue'

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync('token'),
		userInfo: uni.getStorageSync('userInfo'),
		isCustomerPage: uni.getStorageSync('isCustomerPage'),
		cardId: uni.getStorageSync('cardId'),
		cardInfo: {}
	},
	getters: {
		// 该角色是不是消费者
		isCustomer(state) {
			return state.userInfo.roleType === 0
		},
		defaultName: (state) => {
			if (state.userInfo) {
				const {
					userId,
					nickName
				} = state.userInfo;
				if (userId && nickName) {
					return nickName
				}
				if (userId) {
					return "微信用户" + userId.slice(-8)
				}
				return "微信用户"
			}
		},
		cardInfo: (state) => state.cardInfo
	},
	actions: {
		setCardId({
			dispatch,
			commit,
			state
		}, cardId) {
			state.cardId = cardId;
			uni.setStorageSync('cardId', cardId)
		},
		setCardInfo({
			state
		}, detail) {
			state.cardInfo = detail || {};
		},
		setToken({
			dispatch,
			commit,
			state
		}, token) {
			state.token = token;
			uni.setStorageSync('token', token)
		},
		setUserInfo({
			dispatch,
			commit,
			state
		}, userInfo) {
			state.userInfo = userInfo
			uni.setStorageSync('userInfo', userInfo)
		},
		setIsCustomerPage({
			state
		}, value) {
			if (value !== undefined) {
				state.isCustomerPage = value
			} else {
				const status = state.isCustomerPage
				state.isCustomerPage = !status

			}
			uni.setStorageSync('isCustomerPage', state.isCustomerPage)

		}
	}
})
export default store